package com.zgjkhis.modules.outpatient.rest;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.basicsetting.domain.OtherCharge;
import com.zgjkhis.modules.basicsetting.domain.Treatment;
import com.zgjkhis.modules.basicsetting.service.OtherChargeService;
import com.zgjkhis.modules.basicsetting.service.TreatmentService;
import com.zgjkhis.modules.basicsetting.service.dto.AdditionalQueryCriteria;
import com.zgjkhis.modules.basicsetting.service.dto.DrugInfoQueryCriteria;
import com.zgjkhis.modules.charges.domain.PrescriptionCharge;
import com.zgjkhis.modules.charges.domain.PrescriptionOrder;
import com.zgjkhis.modules.charges.domain.PrescriptionRefund;
import com.zgjkhis.modules.charges.domain.PrescriptionRefundDetail;
import com.zgjkhis.modules.charges.service.PrescriptionChargeService;
import com.zgjkhis.modules.charges.service.PrescriptionRefundDetailService;
import com.zgjkhis.modules.charges.service.PrescriptionRefundService;
import com.zgjkhis.modules.charges.service.PrescriptionService;
import com.zgjkhis.modules.charges.service.dto.PrescriptionQueryCriteria;
import com.zgjkhis.modules.enums.AdditionalEnum;
import com.zgjkhis.modules.enums.OrderStatusEnum;
import com.zgjkhis.modules.outpatient.dto.*;
import com.zgjkhis.modules.outpatient.service.AccountService;
import com.zgjkhis.modules.outpatient.service.dto.OrderDto;
import com.zgjkhis.modules.outpatient.service.dto.RecipelDto;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.*;
import com.zgjkhis.modules.system.service.*;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "门诊：处方管理")
@RestController
@RequestMapping("/api/recipel")
public class RecipelController {

    private static final String ENTITY_NAME = "recipel";
    private final AccountService accountService;
    private final ConsultService consultService;
    private final TreatmentService treatmentService;
    private final OtherChargeService otherChargeService;
    private final DrugService drugService;
    private final PrescriptionService prescriptionService;
    private final InfoService infoService;
    private final InfoDrugService infoDrugService;
    private final PrescriptionChargeService prescriptionChargeService;
    private final OrderService orderService;
    private final PrescriptionRefundDetailService prescriptionRefundDetailService;
    private final PrescriptionRefundService prescriptionRefundService;
    private final OrderAddressService orderAddressService;

    public RecipelController(AccountService accountService, ConsultService consultService, TreatmentService treatmentService, OtherChargeService otherChargeService, DrugService drugService, PrescriptionService prescriptionService, InfoService infoService, InfoDrugService infoDrugService, PrescriptionChargeService prescriptionChargeService, OrderService orderService, PrescriptionRefundDetailService prescriptionRefundDetailService, PrescriptionRefundService prescriptionRefundService, OrderAddressService orderAddressService) {
        this.accountService = accountService;
        this.consultService = consultService;
        this.treatmentService = treatmentService;
        this.otherChargeService = otherChargeService;
        this.drugService = drugService;
        this.prescriptionService = prescriptionService;
        this.infoService = infoService;
        this.infoDrugService = infoDrugService;
        this.prescriptionChargeService = prescriptionChargeService;
        this.orderService = orderService;
        this.prescriptionRefundDetailService = prescriptionRefundDetailService;
        this.prescriptionRefundService = prescriptionRefundService;
        this.orderAddressService = orderAddressService;
    }

    @Log("开处方")
    @PostMapping(value = "/open")
    @ApiOperation("开处方")
    public ResponseEntity<Object> open(@RequestBody String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        Consult consult = consultService.getById(id);
        if (CharUtil.isEmpty(consult)) {
            throw new ServiceException("查无此门诊");
        }
        //待就诊==》就诊中
        if (consult.getConsultStatus() == 1) {
            Consult temp = new Consult();
            temp.setId(consult.getId());
            temp.setConsultStatus(2);
            temp.setMorbidityDate(new Date());
            consultService.updateById(temp);
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("问诊详情")
    @PostMapping(value = "/detail")
    @ApiOperation("问诊详情")
    //    @PreAuthorize("@el.check('account:datail')")
    public ResponseEntity<RecipelDto> detail(@RequestBody String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        Consult consult = consultService.getById(id);
        if (CharUtil.isEmpty(consult)) {
            throw new ServiceException("查无此门诊");
        }
        //待就诊==》就诊中
        if (consult.getConsultStatus() == 1) {
            Consult temp = new Consult();
            temp.setId(consult.getId());
            temp.setConsultStatus(2);
            temp.setMorbidityDate(new Date());
            consultService.updateById(temp);
        }
        AccountDto account = accountService.findDetailById(consult.getPatientId());

        RecipelDto recipelDto = new RecipelDto();
        recipelDto.setConsult(consult);
        recipelDto.setAccount(account);

        PrescriptionOrder order = prescriptionService.getOne(Wrappers.<PrescriptionOrder>query().lambda().eq(PrescriptionOrder::getConsultNo, consult.getConsultNo()));
        if (!CharUtil.isEmpty(order)) {
            OrderDetailDto orderDto = new OrderDetailDto();
            BeanUtil.copyProperties(order, orderDto);
            List<Info> infoList = infoService.list(Wrappers.<Info>query().lambda().eq(Info::getOrderNo, order.getOrderNo()).eq(Info::getDeleteFlag, 0));
            for (Info info : infoList) {
                List<InfoDrug> infoDrugList = infoDrugService.list(Wrappers.<InfoDrug>query().lambda().eq(InfoDrug::getInfoNo, info.getInfoNo()).eq(InfoDrug::getDeleteFlag, 0));
                info.setInfoDrugs(infoDrugList);
            }
            orderDto.setInfoList(infoList);
            List<PrescriptionCharge> list = prescriptionChargeService.list(Wrappers.<PrescriptionCharge>query().lambda().eq(PrescriptionCharge::getOrderNo, order.getOrderNo()));
            orderDto.setPrescriptionChargeList(list);
            recipelDto.setOrderDetailDto(orderDto);

            if (!CharUtil.isEmpty(order.getDeliveryType()) && order.getDeliveryType() == 1) {
                OrderAddress orderAddress = orderAddressService.getOne(Wrappers.<OrderAddress>query().lambda().eq(OrderAddress::getOrderNo, order.getOrderNo()));
                recipelDto.setOrderAddress(orderAddress);
            }
        }

        return new ResponseEntity<>(recipelDto, HttpStatus.OK);
    }

    @Log("查找附加费用")
    @PostMapping(value = "/findForAdditional")
    @ApiOperation("查找附加费用")
    public ResponseEntity<Object> findForAdditional(@Validated @RequestBody AdditionalQueryCriteria additionalQueryCriteria) {
        additionalQueryCriteria.setPharmacyId(LoginUserUtil.getPharmId());
        if (additionalQueryCriteria.getType().equals(AdditionalEnum.CL.getCode())) {
            List<Drug> drugList = drugService.finListByAdd(additionalQueryCriteria);
            return new ResponseEntity<>(drugList, HttpStatus.OK);
        } else if (additionalQueryCriteria.getType().equals(AdditionalEnum.ZL.getCode())) {
            //治疗项目
            List<Treatment> listByAdd = treatmentService.findListByAdd(additionalQueryCriteria);
            return new ResponseEntity<>(listByAdd, HttpStatus.OK);
        } else if (additionalQueryCriteria.getType().equals(AdditionalEnum.JCJY.getCode()) || additionalQueryCriteria.getType().equals(AdditionalEnum.OHTER.getCode())) {
            //检验检查 2 其他费用 4
            List<OtherCharge> chargeList = otherChargeService.findListByAdd(additionalQueryCriteria);
            return new ResponseEntity<>(chargeList, HttpStatus.OK);
        } else {
            throw new ServiceException("查群类型错误");
        }
    }

    @Log("查询药品")
    @PostMapping(value = "/findDrug")
    @ApiOperation("查询药品")
    public ResponseEntity<List<Drug>> findDrug(@Validated @RequestBody DrugInfoQueryCriteria drugInfoQueryCriteria) {
        drugInfoQueryCriteria.setPharmacyId(LoginUserUtil.getPharmId());
        List<Drug> drugList = drugService.findDrugByInfoType(drugInfoQueryCriteria);
        return new ResponseEntity<>(drugList, HttpStatus.OK);
    }


    @Log("完成处方")
    @PostMapping(value = "/saveOrder")
    @ApiOperation("完成处方")
    public ResponseEntity<Object> saveOrder(@Validated @RequestBody OrderDto orderDto) {
        prescriptionService.saveOrder(orderDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("删除处方")
    @PostMapping(value = "/deleteInfo")
    @ApiOperation("删除处方")
    public ResponseEntity<Object> deleteInfo(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("请选择需要删除的处方id");
        }
        Info info = infoService.getById(id);
        if (CharUtil.isEmpty(info) || info.getDeleteFlag() != 0) {
            throw new ServiceException("处方不存在,删除失败");
        }
        if (info.getPayStatus() != 0) {
            throw new ServiceException("该处方不是未支付状态,不能删除");
        }

        int count = infoDrugService.queryInfoDrugByInfoNo(info.getInfoNo());
        if (count > 0) {
            throw new ServiceException("该处方中存在支付过的药品,不能删除");
        }
        Info temp = new Info();
        temp.setId(info.getId());
        temp.setDeleteFlag(1);
        infoService.updateById(temp);
        infoDrugService.update(Wrappers.<InfoDrug>update().lambda().set(InfoDrug::getDeleteFlag, 1).eq(InfoDrug::getInfoNo, info.getInfoNo()));
        //修改订单价格统计
        prescriptionService.updateOrderForMoney(info.getOrderNo(), null);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }


    @Log("处方作废")
    @PostMapping(value = "/removeInfo")
    @ApiOperation("处方作废")
    public ResponseEntity<Object> removeInfo(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("请选择需要删除的处方id");
        }
        Info info = infoService.getById(id);
        if (CharUtil.isEmpty(info) || info.getDeleteFlag() != 0) {
            throw new ServiceException("处方不存在,作废失败");
        }
        Order order = orderService.getOne(Wrappers.<Order>query().lambda().eq(Order::getOrderNo, info.getOrderNo()));
        if (order.getOrderType() == 1 && info.getPayStatus() != 0) {
            throw new ServiceException("该处方不是未支付状态,不能删除");
        } else if (order.getOrderType() == 2 && info.getPayStatus() != 1) {
            throw new ServiceException("该处方不是支付状态,不能删除");
        }

        if (order.getOrderType() == 1) {
            int count = infoDrugService.queryInfoDrugByInfoNo(info.getInfoNo());
            if (count > 0) {
                throw new ServiceException("该处方中存在支付过的药品,不能删除");
            }
        }

        Info temp = new Info();
        temp.setId(info.getId());
        temp.setOrderCondition(5);
        infoService.updateById(temp);
        infoDrugService.update(Wrappers.<InfoDrug>update().lambda().set(InfoDrug::getIsCancel, 1).eq(InfoDrug::getInfoNo, info.getInfoNo()));
        //修改订单价格统计
        prescriptionService.updateOrderForMoney(info.getOrderNo(), null);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("删除处方药品")
    @PostMapping(value = "/deleteInfoDrug")
    @ApiOperation("删除处方药品")
    public ResponseEntity<Object> deleteInfoDrug(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("请选择需要删除的处方药品id");
        }
        InfoDrug infoDrug = infoDrugService.getById(id);
        if (CharUtil.isEmpty(infoDrug)) {
            throw new ServiceException("药品不存在,删除失败");
        }
        if (infoDrug.getStatus() != 0) {
            throw new ServiceException("该药品不是未支付状态,不能删除");
        }
        InfoDrug temp = new InfoDrug();
        temp.setId(infoDrug.getId());
        temp.setDeleteFlag(1);
        infoDrugService.updateById(temp);
        //修改订单价格统计
        prescriptionService.updateOrderForMoney(infoDrug.getOrderNo(), null);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("分页查询本地处方单")
    @ApiOperation("分页查询本地处方单")
    @GetMapping(value = "/getPrescriptions")
    public ResponseEntity<PageResult<LocalOrderDto>> getPrescriptions(PrescriptionQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        if (criteria.getOrderCondition() != null && criteria.getOrderCondition() == 34) {
            criteria.setOrderCondition(null);
            criteria.setPayStatus(34);
        }
        return new ResponseEntity<>(this.prescriptionService.findAllByPageableForLocal(criteria, pageable), HttpStatus.OK);
    }

    @Log("查看本地处方单详情")
    @GetMapping(value = "/getDetail")
    @ApiOperation("查看本地处方单详情")
    //    @PreAuthorize("@el.check('supplier:datail')")
    public ResponseEntity<LocalOrderDetailDto> getDetail(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        PrescriptionOrder order = this.prescriptionService.getOne(Wrappers.<PrescriptionOrder>query().lambda().eq(PrescriptionOrder::getId, id).eq(PrescriptionOrder::getPharmacyId, LoginUserUtil.getPharmId()));
        if (CharUtil.isEmpty(order)) {
            order = this.prescriptionService.getOne(Wrappers.<PrescriptionOrder>query().lambda().eq(PrescriptionOrder::getOrderNo, id).eq(PrescriptionOrder::getPharmacyId, LoginUserUtil.getPharmId()));
        }
        if (CharUtil.isEmpty(order)) {
            throw new ServiceException("查无此订单");
        }
        //挂号信息
        Consult consult = consultService.getOne(Wrappers.<Consult>query().lambda().eq(Consult::getConsultNo, order.getConsultNo()));
        //处方信息
        List<Info> infos = infoService.list(Wrappers.<Info>query().lambda().eq(Info::getOrderNo, order.getOrderNo()).eq(Info::getDeleteFlag, 0));
        for (Info info : infos) {
            List<InfoDrug> infoDrugs = infoDrugService.queryInfoDrugListByInfoNo(info.getInfoNo());
            info.setInfoDrugs(infoDrugs);
        }
        order.setInfos(infos);
        LocalOrderDetailDto localOrderDetailDto = new LocalOrderDetailDto();
        if (order.getPayStatus() == 3 || order.getPayStatus() == 4) {
            //邮费是否退款
            long count = prescriptionRefundDetailService.count(Wrappers.<PrescriptionRefundDetail>query().lambda().eq(PrescriptionRefundDetail::getOrderNo, order.getOrderNo()).eq(PrescriptionRefundDetail::getRefundType, 4));
            if (count > 0) {
                localOrderDetailDto.setRefundExp(true);
            }
            //其他费用是否退款
            long count1 = prescriptionRefundDetailService.count(Wrappers.<PrescriptionRefundDetail>query().lambda().eq(PrescriptionRefundDetail::getOrderNo, order.getOrderNo()).eq(PrescriptionRefundDetail::getRefundType, 5));
            if (count1 > 0) {
                localOrderDetailDto.setRefundOtherFee(true);
            }

            //退款原因
            List<PrescriptionRefund> prescriptionRefunds = new ArrayList<>();
            if (order.getPayStatus() == 3 || order.getPayStatus() == 4) {
                prescriptionRefunds = prescriptionRefundService.list(Wrappers.<PrescriptionRefund>query().lambda().eq(PrescriptionRefund::getOrderNo, order.getOrderNo()));
            }
            localOrderDetailDto.setPrescriptionRefunds(prescriptionRefunds);
        }
        if (!CharUtil.isEmpty(order.getDeliveryType()) && order.getDeliveryType() == 1) {
            OrderAddress orderAddress = orderAddressService.getOne(Wrappers.<OrderAddress>query().lambda().eq(OrderAddress::getOrderNo, order.getOrderNo()));
            localOrderDetailDto.setOrderAddress(orderAddress);
        }
        localOrderDetailDto.setOrder(order);
        localOrderDetailDto.setConsult(consult);
        return new ResponseEntity<>(localOrderDetailDto, HttpStatus.OK);
    }

    @Log("订单收费")
    @ApiOperation("订单收费")
    @PostMapping(value = "/chargeOrder")
    public ResponseEntity<Object> chargeOrder(@Validated @RequestBody ChargeOrderCriteria criteria) {
        return new ResponseEntity<>(orderService.chargeOrder(criteria), HttpStatus.OK);
    }

    @Log("修改订单状态(已收货)")
    @ApiOperation("修改订单状态(已收货)")
    @PostMapping(value = "/updateOrder")
    public ResponseEntity<Object> updateOrder(@Validated @RequestBody String orderNo) {
        if (CharUtil.isEmpty(orderNo)) {
            throw new ServiceException("订单号不能能为空");
        }
        PrescriptionOrder prescriptionOrder = prescriptionService.getOne(Wrappers.<PrescriptionOrder>query().lambda().eq(PrescriptionOrder::getPharmacyId, LoginUserUtil.getPharmId()).eq(PrescriptionOrder::getOrderStatus, 0).eq(PrescriptionOrder::getOrderNo, orderNo));
        if (CharUtil.isEmpty(prescriptionOrder)) {
            throw new ServiceException("订单不存在");
        }
        PrescriptionOrder tempOrder = new PrescriptionOrder();
        tempOrder.setId(prescriptionOrder.getId());
        tempOrder.setOrderCondition(4);
        tempOrder.setReceiptUserId(SecurityUtils.getUserId().toString());
        tempOrder.setReceiptUser(LoginUserUtil.getNickName());
        tempOrder.setReceiptUserTime(new Date());
        prescriptionService.updateById(tempOrder);
        infoService.update(Wrappers.<Info>update().lambda().set(Info::getOrderCondition, 4).eq(Info::getOrderNo, prescriptionOrder.getOrderNo()));
        return ResponseEntity.ok(true);
    }

    @Log("订单/处方审核")
    @ApiOperation("订单/处方审核")
    @PostMapping(value = "/checkOrder")
    public ResponseEntity<Object> checkOrder(@Validated @RequestBody OrderUpdateCriteria order) {
        //订单调剂
        if (order.getType() == 1) {
            PrescriptionOrder prescriptionOrder = prescriptionService.getOne(Wrappers.<PrescriptionOrder>query().lambda().eq(PrescriptionOrder::getPharmacyId, LoginUserUtil.getPharmId()).eq(PrescriptionOrder::getOrderStatus, 0).eq(PrescriptionOrder::getOrderNo, order.getCodeNo()));
            if (CharUtil.isEmpty(prescriptionOrder)) {
                throw new ServiceException("订单不存在");
            }
            PrescriptionOrder tempOrder = new PrescriptionOrder();
            tempOrder.setId(prescriptionOrder.getId());
            //审核通过
            tempOrder.setOrderCondition(OrderStatusEnum.TJZ.getCode());
            tempOrder.setCheckUserId(SecurityUtils.getUserId().toString());
            tempOrder.setCheckUser(LoginUserUtil.getNickName());
            tempOrder.setCheckTime(new Date());
            infoService.update(
                    Wrappers.<Info>update().lambda().set(Info::getOrderCondition, OrderStatusEnum.TJZ.getCode())
                            .set(Info::getCheckUserId, SecurityUtils.getUserId().toString()).set(Info::getCheckUser, LoginUserUtil.getNickName())
                            .set(Info::getCheckTime, new Date()).eq(Info::getOrderNo, prescriptionOrder.getOrderNo()).eq(Info::getOrderCondition, 1).eq(Info::getDeleteFlag, 0)
            );
            prescriptionService.updateById(tempOrder);

        } else if (order.getType() == 2) {
            Info info = infoService.getOne(Wrappers.<Info>query().lambda().eq(Info::getDeleteFlag, 0).eq(Info::getInfoNo, order.getCodeNo()));
            if (CharUtil.isEmpty(info)) {
                throw new ServiceException("处方不存在");
            }
            if (info.getOrderCondition() != 1) {
                throw new ServiceException("处方已经审核过了");
            }
            // TODO: 2022/4/21  查询订单是否存在未处理状况

            //审核处方
            infoService.update(
                    Wrappers.<Info>update().lambda().set(Info::getOrderCondition, 2)
                            .set(Info::getCheckUserId, SecurityUtils.getUserId().toString()).set(Info::getCheckUser, LoginUserUtil.getNickName())
                            .set(Info::getCheckTime, new Date()).eq(Info::getId, info.getId())
            );
            //查找是否存在未审核的处方
            long count = infoService.count(Wrappers.<Info>query().lambda().eq(Info::getOrderCondition, 1).eq(Info::getDeleteFlag, 0).eq(Info::getOrderNo, info.getOrderNo()));
            //没有了，订单状态改成调剂中
            if (count <= 0) {
                orderService.update(Wrappers.<Order>update().lambda().set(Order::getOrderCondition, 2)
                        .set(Order::getCheckUserId, SecurityUtils.getUserId().toString()).set(Order::getCheckUser, LoginUserUtil.getNickName())
                        .set(Order::getCheckTime, new Date()).eq(Order::getOrderNo, info.getOrderNo()));
            }

        } else {
            throw new ServiceException("审核方式错误");
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }


    @Log("调剂订单")
    @ApiOperation("调剂订单")
    @PostMapping(value = "/confirmOrder")
    public ResponseEntity<Object> confirmOrder(@Validated @RequestBody OrderUpdateCriteria order) {
        if (order.getType() == 1) {
            //订单调剂
            Order tempOrder = orderService.getOne(Wrappers.<Order>query().lambda().eq(Order::getOrderNo, order.getCodeNo()).eq(Order::getPharmacyId, LoginUserUtil.getPharmId()));

            if (CharUtil.isEmpty(tempOrder)) {
                throw new ServiceException("查无此订单");
            }
            if (tempOrder.getOrderCondition() != 2) {
                throw new ServiceException("非调剂中的订单不能调剂");
            }
            List<InfoDrug> list = infoDrugService.list(Wrappers.<InfoDrug>query().lambda().eq(InfoDrug::getOrderNo, tempOrder.getOrderNo()).eq(InfoDrug::getIsCirulation, 0));
            orderService.confirmOrder(list, tempOrder, null);
        } else if (order.getType() == 2) {
            //处方调剂
            Info info = infoService.getOne(Wrappers.<Info>query().lambda().eq(Info::getInfoNo, order.getCodeNo()).eq(Info::getDeleteFlag, 0));
            if (CharUtil.isEmpty(info)) {
                throw new ServiceException("查无此处方");
            }
            if (info.getOrderCondition() != 2) {
                throw new ServiceException("非调剂中的处方不能调剂");
            }
            List<InfoDrug> list = infoDrugService.list(Wrappers.<InfoDrug>query().lambda().eq(InfoDrug::getInfoNo, info.getInfoNo()).eq(InfoDrug::getIsCirulation, 0));
            orderService.confirmOrder(list, null, info);
        } else {
            throw new ServiceException("调剂方式错误");
        }
        return ResponseEntity.ok(true);
    }


    @Log("订单退费")
    @ApiOperation("订单退费")
    @PostMapping(value = "/refundOrder")
    public ResponseEntity<Object> refundOrder(@Validated @RequestBody RefundOrderDto refundOrderDto) {
        if (refundOrderDto.getRefundType() == 0) {
            if (CharUtil.isEmpty(refundOrderDto.getInfoIds()) || refundOrderDto.getInfoIds().size() <= 0) {
                throw new ServiceException("请选择退款内容");
            }
        }
        Order order = orderService.getOne(Wrappers.<Order>query().lambda().eq(Order::getOrderNo, refundOrderDto.getOrderNo()).eq(Order::getPharmacyId, LoginUserUtil.getPharmId()));
        if (CharUtil.isEmpty(order)) {
            throw new ServiceException("订单不存在");
        }
        if (order.getPayStatus() == 0) {
            throw new ServiceException("订单未支付,不能退款");
        }
        if (refundOrderDto.getRefundType() == 1 && order.getPayStatus() == 3) {
            throw new ServiceException("部分退款订单,不能退全款");
        }
        if (order.getPayStatus() == 4) {
            throw new ServiceException("订单已退费,不能退款");
        }

        return orderService.refundLocalOrder(refundOrderDto, order);
    }

    @Log("待审方数量")
    @ApiOperation("待审方数量")
    @GetMapping(value = "/waitCheck")
    public ResponseEntity<Integer> waitCheck(Integer prescriptionType) {
        String pharmacyId = LoginUserUtil.getPharmId();
        return ResponseEntity.ok(this.prescriptionService.waitCheck(pharmacyId, prescriptionType));
    }

    @Log("待打印数量")
    @ApiOperation("待打印数量")
    @GetMapping(value = "/waitPrint")
    public ResponseEntity<Integer> waitPrint(Integer prescriptionType) {
        String pharmacyId = LoginUserUtil.getPharmId();
        return ResponseEntity.ok(this.prescriptionService.waitPrint(pharmacyId, prescriptionType));
    }

    @Log("待退费数量")
    @ApiOperation("待退费数量")
    @GetMapping(value = "/waitRefund")
    public ResponseEntity<Integer> waitRefund(Integer prescriptionType) {
        String pharmacyId = LoginUserUtil.getPharmId();
        return ResponseEntity.ok(this.prescriptionService.waitRefund(pharmacyId, prescriptionType));
    }

    @Log("退款库存处理")
    @ApiOperation("退款库存处理")
    @PostMapping(value = "/comfireStock")
    public ResponseEntity<Object> comfireStock(@Validated @RequestBody RefundComfireDto refundComfireDto) {
        PrescriptionOrder order = prescriptionService.getOne(Wrappers.<PrescriptionOrder>query().lambda().eq(PrescriptionOrder::getOrderNo, refundComfireDto.getOrderNo()));
        if (CharUtil.isEmpty(order)) {
            throw new ServiceException("查无此单");
        }
        if (order.getPayStatus() != 3 && order.getPayStatus() != 4) {
            throw new ServiceException("此订单没有退款,操作失败");
        }

        prescriptionService.comfireStock(order, refundComfireDto);


        return ResponseEntity.ok(true);
    }


    @Log("订单补发")
    @ApiOperation("订单补发")
    @PostMapping(value = "/reissue")
    public ResponseEntity<Object> reissue(@Validated @RequestBody ReissueDto reissueDto) {
        Info info = infoService.getOne(Wrappers.<Info>query().lambda().eq(Info::getInfoNo, reissueDto.getInfoNo()));
        if (CharUtil.isEmpty(info)) {
            throw new ServiceException("查无此处方");
        }
        prescriptionService.reissue(info, reissueDto);
        return null;
    }


}
